package org.etlt.check;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.etlt.job.JobContext;

import java.io.*;
import java.util.Date;
import java.util.TimeZone;

public class CheckResultVelocityReporter extends CheckResultReporter {
    public static final String UTF_8 = "utf8";

    @Override
    public void report(JobContext jobContext, CheckList checkList) {
//        Velocity.setProperty("resource.loader.file.path", "");
        Velocity.setProperty(
                "resource.loader.file.class", ClasspathResourceLoader.class.getName());
        Velocity.init();
        VelocityContext context = new VelocityContext();
        context.put("checklist_name", checkList.getName());
        context.put("checkpoint_results", checkList.getCheckResults());
        context.put("date", formatDate(new Date()));
        context.put("jobContext", jobContext);

        String htmlLocation = getResourceLocation(jobContext, checkList.getName() + "-report.html");
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlLocation)))) {
            String templateLocation =  "META-INF/checklist-report-html.vm";
            Velocity.mergeTemplate(templateLocation, UTF_8, context, writer);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        if(checkList.getSetting().getReportType() == ReportType.PDF) {
            String pdfLocation = getResourceLocation(jobContext, checkList.getName() + "-report.pdf");
            html2pdf(htmlLocation, pdfLocation);
        }
    }

    protected String getResourceLocation(JobContext context, String fileName) {
        File file = new File(context.getContextRoot() + File.separator + fileName);
        return file.getAbsolutePath();
    }

    protected String formatDate(Date date) {
        return DateFormatUtils.format(date, "yyyy/MM/dd HH:mm:ss", TimeZone.getDefault());
    }

    protected void html2pdf(String html, String pdf) {
        try {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(pdf)));
            ConverterProperties props = new ConverterProperties();
            //字体
            props.setFontProvider(new FontProvider());
            props.getFontProvider().addFont(getFontBytes("META-INF/fonts/simhei.ttf"));

            HtmlConverter.convertToPdf(new FileInputStream(html), pdfDocument, props);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    protected byte[] getFontBytes(String classpathResource) throws IOException {
        try (InputStream inputStream =
                     getClass().getClassLoader().getResourceAsStream(classpathResource)) {
            return IOUtils.toByteArray(inputStream);
        }
    }

}
